package com.android.xk.mask

import android.content.Context
import android.content.Intent
import androidx.multidex.MultiDexApplication
import com.apkfuns.log2file.LogFileEngineFactory
import com.apkfuns.logutils.LogLevel
import com.apkfuns.logutils.LogUtils
import com.danikula.videocache.HttpProxyCacheServer
import com.facebook.drawee.backends.pipeline.Fresco
import io.realm.Realm


class App : MultiDexApplication() {

    private val proxy: HttpProxyCacheServer? = null


    companion object {
        @JvmStatic
        fun getProxy(context: Context): HttpProxyCacheServer {
            val app = context.applicationContext as App
            return app.proxy ?: app.newProxy()
        }

        var deviceId: String? = null
    }

    private fun restartApp() {
        val intent = Intent(applicationContext, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
        android.os.Process.killProcess(android.os.Process.myPid())
    }

    override fun onCreate() {
        super.onCreate()
        Realm.init(this)
        Fresco.initialize(this)
        LogUtils.getLog2FileConfig().configLog2FileEnable(true)
            .configLog2FilePath("/sdcard/logs/")
            .configLog2FileLevel(LogLevel.TYPE_INFO)
            .configLog2FileNameFormat("%d{yyyyMMdd}.txt")
            .configLogFileEngine(LogFileEngineFactory(this))
        Thread.setDefaultUncaughtExceptionHandler { t, e -> restartApp() }
    }

    private fun newProxy(): HttpProxyCacheServer {
        return HttpProxyCacheServer(this)
    }
}